package com.easyexcel;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;

public class TestDyncEasyExcel {

	public static void main(String[] args) {
		String fileName =  "D:\\AijunTao\\Desktop\\easyexcel\\DyncData.xlsx";
		
		 // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景设置为红色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short)20);
        headWriteCellStyle.setWriteFont(headWriteFont);
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 背景绿色
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short)20);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
            new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
		
        EasyExcel.write(fileName)//.registerWriteHandler(horizontalCellStyleStrategy)
        		.registerWriteHandler(new SimpleColumnWidthStyleStrategy(15)) // 简单的列宽策略，列宽20
                // 这里放入动态头
                .head(getHead()).sheet("Sheet1")
                // 当然这里数据也可以用 List<List<String>> 去传入
                .doWrite(getData());

	}
	
	public static List<List<String>> getData(){
        List<List<String>> total=new ArrayList<>();
        List<String> list=new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            list.add("我是第"+i+"列!");
        }
        total.add(list);
        return total;
    }
	
	public static List<List<String>> getHead2(){
		List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<>();
        head0.add("Id" );
 
        List<String> head1 = new ArrayList<>();
        head1.add("名称");
 
        List<String> head2 = new ArrayList<>();
        head2.add("尺寸");
 
 
        list.add(head0);
        list.add(head1);
        list.add(head2);
 
        List<String> productHead= new ArrayList<>();
        productHead.add("aaa");
        productHead.add("bbb");
        productHead.add("ccc");
 
        List<String> head3 = new ArrayList<>();
        for (int i = 1; i < 5; i++) {
            for (String s : productHead) {
                head3 = new ArrayList<>();
                head3.add("子产品"+i);
                head3.add(s);
                list.add(head3);
            }
        }
 
        return list;
    }
	
	public static List<List<String>> getHead(){
		List<List<String>> list = new ArrayList<List<String>>();
		for(int i=0; i<15; i++) {
			List<String> head0 = new ArrayList<>();
	        head0.add("Head" + i);
	        list.add(head0);
		}
        return list;
    }

}
